<template>
  <div class="my-counter">
    <button
      type="button"
      class="btn btn-light"
      @click="jian"
      :disabled="count === 1"
    >
      -
    </button>
    <input
      type="number"
      class="form-control inp"
      :value="count"
      @change="changeIpt"
    />
    <button type="button" class="btn btn-light" @click="add">+</button>
  </div>
</template>

<script>
export default {
  props: {
    count: {
      type: Number,
      default: 1,
    },
  },
  methods: {
    jian() {
      this.$emit('changeCount', this.count - 1)
    },
    add() {
      this.$emit('changeCount', this.count + 1)
    },
    changeIpt(e) {
      let count = e.target.value
      if (count < 1) {
        count = 1
        e.target.value = 1
      }
      this.$emit('changeIpt', parseInt(count))
    },
  },
}
</script>

<style lang="less" scoped>
.my-counter {
  display: flex;
  .inp {
    width: 45px;
    text-align: center;
    margin: 0 10px;
  }
}
</style>
